<template>
  <div>
    <!--  修改表单-->
    <auditdesign :items="items" :idsarr="idsarr" @save="save" />
    <el-dialog title="请输入表单名称" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="表单名称">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="isok">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: 'Formpage',
  data() {
    return {
      items: [],
      bigidarr: [],
      id: null,
      fromid: null,
      fromidarr: [],
      formdata: {},
      idsdata: [],
      dialogFormVisible: false,
      form: {
        name: ''

      },
      idsarr: [] // 表单回显ids
    }
  },
  computed: {
    idarr() {
      return this.$store.state.print.bigidarr
    }

  },

  watch: {
    bigidarr: {
      handler: function(val, oldval) {
        this.bigidarr = val
      }
    }

  },
  created() {
    this.getqueryid()
    this.gitalllist()
  },

  methods: {
    isok() {
      this.$Http.changeverification({
        formName: this.form.name,
        id: this.fromid
      }).then(res => {
        if (res.code === 200) {
          this.$Http.changefrom({
            formName: this.form.name,
            formJson: '',
            formItemIds: this.idsdata,
            id: this.fromid
          }).then(res => {
            if (res.code === 200) {
              this.dialogFormVisible = false
              console.log(res)
              this.$message({
                type: 'success',
                message: '修改成功'

              })
              this.$router.go(-1)
            } else {
              this.$message({
                type: 'error',
                message: '修改失败'

              })
            }
          }).catch(err => {
            console.log('err', err)
          })
        }
      }).catch(err => {
        console.log('err', err)
      })
    },

    save(data) {
      this.dialogFormVisible = true
      this.idsdata = data.idarr
    },
    // 取所有按钮
    async gitalllist() {
      try {
        const res = await this.$Http.getbtnall()
        const valTemp = []
        const valType = []

        if (res.code === 200) {
          for (let r = 0; r < res.result.length; r++) {
            valType.push({
              type: 'assembly',
              name: res.result[r].formIndexName,
              id: res.result[r].id
            })
            valTemp.push(res.result[r].formIndexJsonObject)
          }

          for (var c = 0; c < valType.length; c++) {
            const item = {
              type: valType[c],
              data: valTemp[c]
            }
            this.items.push(item)
          }
        }
      } catch (err) {
        console.log('err', err)
      }
    },
    // 获取id
    getqueryid() {
      this.id = this.$route.query.id
      this.fromid = this.$route.query.fromid
      this.form.name = this.$route.query.fromname

      this.getformids()
    },
    async getformids() {
      try {
        const res = await this.$Http.frombianji(this.fromid)
        if (res.code === 200) {
          this.idsarr = res.result.formItemIds
        } else {
          this.$message({
            type: 'success',
            message: res.message
          })
        }
      } catch (err) {
        console.log('err', err)
      }
    }

  }
}
</script>

<style scoped>

</style>
